package org.service.car.activity;

import java.sql.SQLException;
import java.util.ArrayList;

import org.service.car.R;
import org.service.car.database.DatabaseHelper;
import org.service.car.helper.MostrarFornecedor;
import org.service.car.helper.UserLocation;
import org.service.car.pojo.Fornecedor;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;

public class MapaFornecedores extends FragmentActivity {

	private GoogleMap map;
	private SupportMapFragment fragment;
	private UserLocation userLocation = null;
	private MostrarFornecedor mostrar = null;
	private static String IDS_CATEGORIAS = "idsCategoria";
	private ArrayList<Integer> idsCategorias;
	private static final String HTTP_WWW = "http://www.";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mapa_fornecedores);
		fragment = (SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.map);
		map = fragment.getMap();
		LatLngBounds bounds = map.getProjection().getVisibleRegion().latLngBounds;
		Intent intent = getIntent();
		idsCategorias = intent.getIntegerArrayListExtra(IDS_CATEGORIAS);
		mostrar = new MostrarFornecedor(map, getApplicationContext(), idsCategorias, bounds);
		mostrar.run();
		
		userLocation = new UserLocation(getApplicationContext(), map);
		userLocation.atualizar();
		
		setUpMap();
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.mapa_fornecedores, menu);
		return true;
	}

	private void setUpMap() {
		map.setMyLocationEnabled(true);
		map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

			public void onInfoWindowClick(Marker marker) {
				String[] title = marker.getTitle().split("-");
				Fornecedor fornecedor = null;
				try {
					fornecedor = DatabaseHelper
							.getHelper(getApplicationContext())
							.getFornecedorDao()
							.queryForId(Integer.valueOf(title[0]));
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (fornecedor != null)
					if (!fornecedor.getUrl().isEmpty() && fornecedor.getUrl().startsWith(HTTP_WWW))
						startActivity(new Intent(Intent.ACTION_VIEW, Uri
								.parse(fornecedor.getUrl())));
			}
		});

		/*map.setOnMyLocationChangeListener(new OnMyLocationChangeListener() {

			@Override
			public void onMyLocationChange(Location location) {
				userLatLng = new LatLng(location.getLatitude(), location
						.getLongitude());
			}
		});
		

		map.setOnCameraChangeListener(new OnCameraChangeListener() {
			@Override
			public void onCameraChange(CameraPosition cameraPosition) {
				LatLngBounds bounds = map.getProjection().getVisibleRegion().latLngBounds;
				if (userLatLng != null && cameraPosition != null) {
					DecimalFormat df = new DecimalFormat("##,###");
					Double userLat =  Double.parseDouble(df.format(userLatLng.latitude));
					Long userLong = Math.round(userLatLng.longitude);
					Toast.makeText(
							getApplicationContext(),
							String.valueOf((userLatLng == cameraPosition.target)
									+ "\nUser LatLong: "
									+ userLat.toString() + " - "
									+ userLong.toString()
									+ "\nCameraLatLong: "
									+ cameraPosition.target.toString()),
							Toast.LENGTH_LONG).show();
					if (userLatLng == cameraPosition.target) {
						mostrar = new MostrarFornecedor(map,
								getApplicationContext(),
								CategoriaType.getIds(), bounds);
					}
				}
			}
		});
*/
	}
}